class gpage2 extends BaseScreen {
    public constructor() {
        super();
        this.skinName = "resource/eui_skins/page2.exml";      
    }

	 protected childrenCreated(): void {
        super.childrenCreated();
        this.init_view();
    }

	private group_tip:eui.Group;
	private img_p2_start_btn:eui.Image;
	private game_time:number = 30000;
	private gameing_time:number = 0;
	//1 等待  2 进行中 3 结束 
	private game_state:number = 0;

	private group_mv:eui.Group;
	private lab_point:eui.Label;
	private lab_time:eui.Label;
	private lab_source:eui.Label;
	private lab_mv:eui.Label;
	

	private rb_list:Array<rb_title>;
	private rb_create_list:Array<rb_title>;
	private remoev_arr:Array<number>;
	public tong_list:Array<eui.Image>;

	public group_result:eui.Group;
	private lab_result:eui.Label;
	private img_p2_next_btn:eui.Image;
	private group_img_tip:eui.Group;
	private group_game:eui.Group;

	public create_rb_time:number = 0;
	public is_stag:boolean = false;
	private default_speed:number = 90;
	public rb_speed:number = 90;
	// public group_loading:screenLoad;
	public init_view(){

		this.img_p2_start_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.start_hander, this);
		this.img_p2_next_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.end_hander, this);
        this.addEventListener(egret.Event.ENTER_FRAME, this.OnEnterFrame, this);
		this.group_tip.visible = true;
		this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchMoveHandle,this);
		this.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.touch_end_hander, this);
		this.addEventListener(egret.TouchEvent.TOUCH_END, this.touch_end_hander, this);	

		this.rb_list = new Array();
		this.remoev_arr = new Array();
		this.rb_create_list = new Array();
		this.tong_list = new Array();

		this.tong_list.push(this['img_p2_t1']);
		this.tong_list.push(this['img_p2_t2']);
		this.tong_list.push(this['img_p2_t3']);
		this.tong_list.push(this['img_p2_t4']);

		for(var i =0;i<3;i++){
			this.rb_create_list.push(new rb_title(this));
		}

		utils.CoreData.instance.cur_source = 0;
		this.lab_source.text = utils.CoreData.instance.cur_source.toString();
		this.lab_mv.visible = false;
		this.group_result.visible = false;
		this.rb_speed = this.default_speed;

		// this.group_loading = new screenLoad();
		// this.addChild(this.group_loading);
		// this.group_loading.close();
	}

	private start_hander(e:egret.TouchEvent){
		this.group_tip.visible = false;
		//rb_title
		this.update_sate(2);
	}

	private end_hander(){
		this.clear();
		utils.ScreenManager.instance.changeToShowScreen(3);
	}

	private update_sate(c_state:number){
		this.game_state = c_state;
		if(c_state == 2){
			this.gameing_time = this.create_rb_time = egret.getTimer();
			this.create_rb();
		}
		else if(c_state == 3){
			this.group_result.visible = true;
			this.group_result.alpha = 0;
			this.lab_result.text = utils.CoreData.instance.cur_source.toString();
			egret.Tween.get(this.group_result).to({'alpha':1},500);
		}
	}

	public add_source(){
		utils.CoreData.instance.cur_source += 10;
		this.lab_source.text = utils.CoreData.instance.cur_source.toString();
		if(!this.lab_mv.visible){
			this.lab_mv.visible = true;
			this.lab_mv.scaleX = this.lab_mv.scaleY = 0.5;
			egret.Tween.get(this.lab_mv).to({'y':-20,'scaleX':1,'scaleY':1},400).call(function(){
				this.lab_mv.visible = false;
				this.lab_mv.y = 88;
			},this);
		}
		
	}

	private create_rb(i_index:number = 0){
		if(this.rb_create_list.length<=0){
			this.rb_create_list.push(new rb_title(this));
		}

		var rb:rb_title = this.rb_create_list.shift();
		

		var cur_img:eui.Image;
		for(var i = this.rb_list.length-1;i>=0;i--){
			if(this.rb_list[i].states == 1){
				cur_img = this.rb_list[i];
				break;
			}
		}
		var startX:number = this.lab_point.x;
		if(cur_img){
			startX = cur_img.x+utils.CoreData.instance.rb_width + 20;
		}

		if(startX < this.lab_point.x){
			startX = this.lab_point.x;
		}

		rb.visible = false;
		this.group_mv.addChild(rb);
		rb.set_info(new egret.Point(startX,this.lab_point.y),100);		
		rb.cur_id = i_index;
		this.rb_list.push(rb);
		rb.visible = true;
		// console.log(' this.rb_create_list='+ this.rb_create_list.length);
		// console.log(' this.rb_list='+ this.rb_list.length);
		
	}

	
	private OnEnterFrame(e: egret.Event) {
        
		if(this.game_state == 2){
			//shift()
			this.remoev_arr.length = 0;
			var rb_len = this.rb_list.length;
			for(var i =0;i<rb_len;i++){
				this.rb_list[i].process();

				if(this.rb_list[i].states == 4){
					this.remoev_arr.push(i);
				}
			}

			
			for(i=0;i<this.remoev_arr.length;i++){
				var r_index = this.remoev_arr[i];
				var rb_t:rb_title = this.rb_list[r_index];				
				this.rb_list.splice(r_index, 1);
				// this.rb_create_list.push(rb_t); 
			}

			var cur_time:number = egret.getTimer();
			if(cur_time - this.create_rb_time > 1100){
				this.create_rb_time = egret.getTimer();
				this.create_rb();
			}

			var t_time:number = cur_time - this.gameing_time;
			if(t_time > this.game_time){
				this.update_sate(3);
				this.lab_time.text = "00:00";
			}
			
			var cur_time = (Math.ceil((this.game_time - t_time)/1000));
			if(cur_time>=10){
				this.lab_time.text = '00:'+cur_time+"";
			}
			else{
				this.lab_time.text = '00:0'+cur_time+"";
			}

			this.rb_speed = this.default_speed + cur_time;
		}

    }

	public touchMoveHandle(e:egret.TouchEvent){
		utils.CoreData.instance.StageX = e.stageX;
		utils.CoreData.instance.StageY = e.stageY;

		// console.log('ex='+e.stageX+'ey='+e.stageY);
	}


	public touch_end_hander(e:egret.TouchEvent){
		this.is_stag = false;
		for(var i =0;i<this.rb_list.length;i++){
			this.rb_list[i].touch_end();
		}
	}



    //转换场景
    private changeScreen(): void {
        this.clear();
        utils.ScreenManager.instance.changeToShowScreen(3);
    }

	public stop_other():void{
		for(var i =0;i<this.rb_list.length;i++){
			this.rb_list[i].touch_end();
		}
	}

	public clear(){
		this.img_p2_start_btn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.start_hander, this);
		this.img_p2_next_btn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.end_hander, this);
        this.removeEventListener(egret.Event.ENTER_FRAME, this.OnEnterFrame, this);
		this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchMoveHandle,this);
		this.removeEventListener(egret.TouchEvent.TOUCH_CANCEL, this.touch_end_hander, this);
		this.removeEventListener(egret.TouchEvent.TOUCH_END, this.touch_end_hander, this);	
	}

	public resize(): void {
        super.resize();

		this.group_img_tip.y = (this.height - this.group_img_tip.height)*0.5;
        this.group_game.y = (this.height - this.group_game.height);
		this.group_result.y = (this.height - this.group_result.height)*0.5;

        this['rect_help'].alpha = 0.6;
    }
}